import {
  api,
  util,
  forms,
  storage
} from "../util.js"
import authorize from './authorize.js'
const app = getApp();
/**
 *
 *九宫格
 *
 */
var squared = {
  /**
   * 获得游戏信息
   */
  getGameInfo(data, callback) {
    var entity = authorize.getEntity() || authorize.getDefault();
    var param = {
      Id: data.id,
      Token: entity.manager.Token,
      ProprietorId: app.dns.proprietorId
    };
    util.post("game.game.info", param,
      function(res) {
        res = res.data;
        if (res.Success) {
          if (callback != undefined && typeof(callback) == "function") {
            callback(res.Content);
          }
        } else {
          util.tips(res.Message);
        }
      }, "3.0", null, false);
  },
  /**
   * 获得中奖记录
   */
  getWinningRecord(data, callback) {
    var entity = authorize.getEntity() || authorize.getDefault();
    var param = {
      Id: data.id,
      Rows: 20,
      ProprietorId: app.dns.proprietorId
    };
    util.post("game.game.winningrecord", param,
      function(res) {
        res = res.data;
        if (res.Success) {
          if (callback != undefined && typeof(callback) == "function") {
            callback(res.Content);
          }
        } else {
          util.tips(res.Message);
        }
      }, "3.0", null, false);
  },
  /**
   * 获得游戏预置信息
   */
  getPreInfo(data, callback) {
    var entity = authorize.getEntity() || authorize.getDefault();
    var param = {
      Id: data.id,
      IsOrderDraw: data.isOrderDraw || 0,
      StoreId: data.storeId || 0,
      Token: entity.manager.Token,
      Terminal: "Moblie",
      ProprietorId: app.dns.proprietorId
    };
    util.post("game.game.play", param,
      function (res) {
        res = res.data;
        if (res.Success) {
          if (callback != undefined && typeof (callback) == "function") {
            callback(res.Content);
          }
        } else {
          util.tips(res.Message);
        }
      }, "3.0", null, false);
  },
  /**
   * 提交
   */
  submitReceivedAward(data, preInfo, callback) {
    var entity = authorize.getEntity() || authorize.getDefault();

    var bo = forms.formValidate(data);
    if (!bo) return;
    var data = forms.getValues(data);
    var awardRecord = `
    <ResultId>${preInfo.ResultId}</ResultId>
    <ContactMan><![CDATA[${data.ContactMan}]]></ContactMan>
    <ContactTel>${data.ContactTel}</ContactTel>
    <ContactAddress>${data.ContactAddress}</ContactAddress>
    `
    var param={
      VisitorId:entity.fkId,
      VisitorFlag:entity.fkFlag,
      AwardRecord:(awardRecord),
      Token: entity.manager.Token,
      ProprietorId: app.dns.proprietorId
    }

    util.post("game.awardrecord.save", param,
      function (res) {
        res = res.data;
        if (res.Success) {
          if (callback != undefined && typeof (callback) == "function") {
            callback(res.Content);
          }
        } else {
          util.tips("失败" + res.Message);
        }
      }, "3.0", null, false);
  },
  /**
   * 列表
   */
  loadList: (page, callback) => {
    var entity = authorize.getEntity();
    var param = util.mergeJsonObject(page, {
      VisitorId: entity.fkId,
      VisitorFlag: entity.fkFlag,
      Token: entity.manager.Token,
      ProprietorId: app.dns.proprietorId
    });
    util.post("game.myprize.page", param, function (res) {
      res = res.data;
      if (res.Success) {
        if (callback != undefined && typeof (callback) == "function") {
          // util.tips("领取成功");
          callback(res.Content);
        }
      } else {
        util.alert(res.Content);
      }
    },"3.0");
  },
  /**
   * 获取上一次领奖信息
   */
  getNearInfo:(callback)=>{
    var entity = authorize.getEntity() || authorize.getDefault();
    var param = {
      FKId: app.dns.proprietorId,
      FKFlag: app.dns.proprietor,
      Token: entity.manager.Token,
      proprietorId: app.dns.proprietorId,
      proprietor:app.dns.proprietor
    };
    util.post("game.awardrecord.latest", param, function (res) {
      res = res.data;
      if (res.Success) {
        if (callback != undefined && typeof (callback) == "function") {
          callback(res.Content);
        }
      } else {
        util.alert(res.Content);
      }
    },"3.0");
  }
}
export {
  squared
}